// Copyright 2006-2007 javascript-array.com

/**
 * Script managing the render of the navigation menu.
 * @author Christophe Sourisse
 */


var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;

$(document).ready(function(){
   
   // Deploy/Undeploy a sub-menu
   $(".navigation > li")
      .mouseover(function(){mopen($(this).children("ul")[0]);})
      .mouseout(function(){mclosetime()});

   // Organize the sub-menus vertically
   $(".navigation > li > ul > li").each(function(){
      if ($("#isLogged").length > 0){
         if ($(this).children("a[class='unlog']").length == 0)
            $(this).after("<br>");
      }
      else {
         if ($(this).children("a[class='log']").length == 0)
            $(this).after("<br>");
      }
   });
      
   // Adapt the navigation menu's content following the log status of the user
   if ($("#isLogged").length > 0)
      $(".navigation .unlog").parent().remove();
   else 
      $(".navigation .log").parent().remove();
   
});


// open hidden layer
function mopen(elem)
{	
   if(elem) {
      
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = elem;
	elem.style.visibility = 'visible';

   }
}
// close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose;